﻿using System;
using System.Linq;
using NGourd.Core.Reports;
using NGourd.CLI;
using System.IO;

namespace NGourd
{
    class NGourdOptions : Options
    {
        public NGourdOptions()
        {
            AddOption(new Option
            {
                ShortForm = "d",
                LongForm = "dry-run",
                Description = "If set to true will build a plan without executing it [defaults to false]",
                ApplyAction = (cfg, val) =>
                {
                    cfg.IsDryRun = (String.IsNullOrEmpty(val) || Boolean.Parse(val));
                }
            });

            AddOption(new Option
            {
                ShortForm = "o",
                LongForm = "output",
                Description = "Select a file to put output in. Currently this is XML which matches an NUnit report",
                ApplyAction = (cfg, fileName) =>
                {
                    cfg.Reporters.Add(new NUnitReporter(new FileSystemService(), fileName));
                }
            });

            AddOption(new Option
            {
                ShortForm = "s",
                LongForm = "feature-set",
                Description = "A comma separated list of the features you want to run",
                ApplyAction = (cfg, featureNames) =>
                {
                    foreach (var featureName in featureNames.Split(','))
                        cfg.FeatureSet.Add(featureName.Trim());
                }
            });

            AddOption(new Option
            {
                ShortForm = "q",
                LongForm = "quiet",
                Description = "Suppress console report output",
                ApplyAction = (cfg, val) =>
                    {
                        cfg.IsInQuietMode = (String.IsNullOrEmpty(val) || Boolean.Parse(val));
                        if (cfg.IsInQuietMode)
                        {
                            // HACK: Other things might talk to the Console. A Console Reporter might get added after this is applied as well.
                            cfg.Reporters = cfg.Reporters.Where(r => r is ConsoleReporter == false).ToList();
                        }
                    }
            });

            AddOption(new Option
            {
                ShortForm = "h",
                LongForm = "help",
                Description = "Displays this message",
                ApplyAction = (cfg, val) =>
                {
                    ShowUsage();
                    Environment.Exit(0);
                }
            });
        }

        public override void ShowUsage()
        {
            Console.WriteLine("NGourd.exe <assembly name> [<assembly name>...] [options]");
            base.ShowUsage();
        }

    }
}
